package com.nk.system.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.nk.system.beans.UsersBean;
import com.nk.system.service.AuthenticationService;
import com.nk.system.service.RegisterService;

/**
 * Servlet implementation class RegisterServlet
 */
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		boolean result = regis(request, response);
		
		if(result == true){
			request.getRequestDispatcher("PredictServlet").forward(request, response);
			/*response.sendRedirect("login.jsp");*/
		}
		else{
			request.getRequestDispatcher("SignupServlet").forward(request, response);
			/*response.sendRedirect("signup.jsp");*/
		}
	}
	
	private boolean regis(HttpServletRequest request, HttpServletResponse response) throws IOException{
		
		String user = request.getParameter("username");
		String pass1 = request.getParameter("password1");
		String pass2 = request.getParameter("password2");
		String fName = request.getParameter("firstName");
		String lName = request.getParameter("lastName");
		String sex = request.getParameter("sex");
		String day = request.getParameter("day");
		String month = request.getParameter("month");
		String year = request.getParameter("year");
		String mail = request.getParameter("mail");
		String school = request.getParameter("school");
		String level = request.getParameter("level");
		String grade = request.getParameter("grade");
		
		int level2 = Integer.parseInt(level);
		float grade2 = Float.parseFloat(grade);
		
		WebApplicationContext con = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext());
		AuthenticationService authenticationService = (AuthenticationService)con.getBean("authenticationService");
		RegisterService registerService = (RegisterService)con.getBean("registerService");
		boolean newUser = authenticationService.authenticationNewUser(user, mail);
		
		HttpSession regisSess = request.getSession();
		regisSess.setAttribute("user", request.getParameter("username"));
		regisSess.setAttribute("fName", request.getParameter("firstName"));
		regisSess.setAttribute("lName", request.getParameter("lastName"));
		regisSess.setAttribute("email", request.getParameter("mail"));
		regisSess.setAttribute("schoolSess", request.getParameter("school"));
		regisSess.setAttribute("levelSess", request.getParameter("level"));
		regisSess.setAttribute("gradeSess", request.getParameter("grade"));
		
		if(newUser == true) {
			if(pass1.equals(pass2) && pass1 != null && pass1.length() >= 4){
				authenticationService.insert(user, pass1, fName, lName, sex, day, month, year, mail, school, level2, grade2);
				UsersBean usersBean = authenticationService.authentication(user, pass1);
				registerService.insert(usersBean.getUsId());
				return true;
			}
		}
		return false;
	}

}
